<?php
namespace app\payqr\model;

use app\BaseModel;


class CardUser extends BaseModel
{
    protected $name = 'longbing_card_user';
    //支付码
    public function pay_list($where,$page,$limit){
       $data = $this->alias( 'a' )
           ->join( 'longbing_card_user_info b', 'a.id = b.fans_id')
           ->where($where)
           ->field('b.name,b.avatar,a.pay_qr,a.avatarUrl,a.id,a.nickName')
           ->order('b.top','asc')
           ->paginate([ 'list_rows' => $limit, 'page' => $page ])->each(function ($val){
                    if(!$val['avatar']){
                        $val['avatar'] =  $val['avatarUrl'];
                    }
                    return $val;
           })
           ->toArray();
       if(!empty($data['data'])){
           foreach ($data['data'] as &$v){
               $v['avatar'] = !empty($v['avatar'])?$v['avatar']:$v['avatarUrl'];
               $v['avatar'] = !empty($v['avatar'])?$v['avatar']:'https://lbqnyv2.migugu.com/defaultAvatar.png';
           }
       }

        $data = transImagesOne($data, ['pay_qr','avatar']);
       return $data;
    }
    public function getPyCode($user_id){
          $data = $this->where('id',$user_id)->field('pay_qr')->find()->toArray();
          $data = transImagesOne($data,['pay_qr']);
          return $data;
    }
    public function getUser($where){
        return $this->where($where)->order('create_time','asc')->find();
    }
    //获取用户所有
    public function userAll($where){
        $data = $this->alias( 'a' )
            ->join( 'longbing_card_formId b', 'a.id = b.user_id')
            ->field('a.id,a.openid,a.is_staff,a.uniacid,b.formId,b.id as bid')
            ->where($where)
            ->group('b.user_id')
            ->select()->toArray();
        return $data;
    }
    //获取用户id
    public function getUserId($where){
        return $this->where($where)->column('id');
    }
    /**
     * @param $type
     * @return mixed
     *
     * 查询某天的客户数量
     */
    public function toDay($where,$time){
        $day = $this->alias( 'a')
            //->join( 'longbing_card_user_info b', 'a.id = b.fans_id')
            ->where($where)->whereDay('a.create_time',$time)->count();
        return $day;
    }
}